<?php

declare(strict_types=1);

namespace App\Controller;


use App\Constants\ErrorCode;
use App\Constants\Redis\Region;
use App\Request\Common\SendSmsCaptchaRequest;
use App\Utils\Excel;
use App\Utils\Log;
use App\Utils\SMS;
use Hyperf\Redis\RedisFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;

class CommonController extends AbstractController
{
    /**
     * 发送手机验证码
     * @param SendSmsCaptchaRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function sendSmsCaptcha(SendSmsCaptchaRequest $request)
    {
        $validator = $request->validated();
        $sms = di(SMS::class);
        $data = $sms->sendCaptcha($validator['phone']);
        return $this->response->success(['state' => $data]);
    }

    public function regions()
    {
        $redis = di(RedisFactory::class)->get('default');
        $res = $redis->get(Region::TREE);
        if (!$res) {
            $command = 'generateRegionTreeCache';
            $params = ["command" => $command];

            $input = new ArrayInput($params);
            $output = new NullOutput();

            $container = \Hyperf\Utils\ApplicationContext::getContainer();

            /** @var Application $application */
            $application = $container->get(\Hyperf\Contract\ApplicationInterface::class);
            $application->setAutoExit(false);

            try {
                $exitCode = $application->find($command)->run($input, $output);
                if ($exitCode) {
                    $res = $redis->get(Region::TREE);
                }
            } catch (\Exception $e) {
                Log::get()->error($e->getTraceAsString());
                return $this->response->fail(ErrorCode::SERVER_ERROR);
            }
        }
        $regions = json_decode($res, true);
        return $this->response->success($regions);
    }

    public function syncEarnings()
    {
        $filename = BASE_PATH . '/public/excel/data.xlsx';
        $res = di(Excel::class)->read('Xlsx', $filename, ['avatar', 'name', 'event', 'money']);
        foreach ($res as &$re) {
            $re['avatar'] = env('APP_HOST') . '/image/' . $re['avatar'];
        }
        return $this->response->success($res);
    }

    public function init(){
        if(env('APP_ENV') == 'prod'){
            return $this->response->fail(ErrorCode::SERVER_ERROR);
        }
        $command = 'AdminInit';
        $params = ["command" => $command];

        $input = new ArrayInput($params);
        $output = new NullOutput();

        $container = \Hyperf\Utils\ApplicationContext::getContainer();

        /** @var Application $application */
        $application = $container->get(\Hyperf\Contract\ApplicationInterface::class);
        $application->setAutoExit(false);

        try {
            $exitCode = $application->find($command)->run($input, $output);
        } catch (\Exception $e) {
            Log::get()->error($e->getTraceAsString());
            return $this->response->fail(ErrorCode::SERVER_ERROR);
        }
        return $this->response->success();
    }
}